<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import= "com.roadmap.struts.action.ActionConst"%>
<%@ page import="com.roadmap.common.RdcResourceBundle" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String user_ssn_id = (String)request.getSession().getAttribute(ActionConst.Aspirant_Id_In_Session);
String user_req_id = (String)request.getParameter(ActionConst.Aspirant_Id_In_Request);
if(user_req_id == null){
	user_req_id = user_ssn_id;
}
String plan_req_id = (String)request.getParameter(ActionConst.Item_Id_In_Request);
if(plan_req_id == null){
	plan_req_id = "";
}
String action = request.getParameter(ActionConst.Method_Symbol);
if(action == null) {
	action = "view";
}
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href="<%=basePath%>"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="pragma" content="no-cache"/>
<meta http-equiv="cache-control" content="no-cache"/>
<meta http-equiv="expires" content="0"/> 
<link href="res/css/layout.css" rel="stylesheet" type="text/css" media="all" />
<link href="res/css/life.css" rel="stylesheet" type="text/css" media="all" />
<link href="res/css/widget.css" rel="stylesheet" type="text/css" media="all" />
<script type="text/javascript" src="dwr/util.js"></script>
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/interface/PlanHandler.js"></script>
<script type="text/javascript" src="dwr/interface/RdcAjax.js"></script>
<script type="text/javascript" src="res/js/util.js"></script>
<script type="text/javascript" src="res/js/func.js"></script>
<script type="text/javascript" src="res/js/widget.js"></script>
<script type="text/javascript" src="res/js/component.js"></script>
<title>
	<logic:present name="plan"><bean:write name='plan' property='name'/> |</logic:present> 计划 | <%=RdcResourceBundle.COMPANY_NAME %>
</title>
</head>

<body>
	<jsp:include page="/admin/myheader.jsp"></jsp:include>
	
	<!-- main body -->
	<div class="mainbody"><div class="manager">
	<jsp:include page="/admin/life/nav_btns.jsp"/>
			
		<!-- middle part: content -->
		<div class="middle-part">
			<div class="section" style="border-bottom: 1px solid #E78F28">
				<div class="title-sp">返回<a href="aspirant.do?uid=<%=user_req_id %>"><bean:write name="<%=ActionConst.Aspirant_Name_In_Request %>"/></a>的<a href="plan.do?act=all&uid=<%=user_req_id %>">计划列表</a></div>
		
				<logic:present name="<%=ActionConst.Visitor_Is_Owner %>">
				<div id="btnSubSec" class="float-right"><input class="dropParentBtn" type="button" value="更多操作" onclick="dropMenu.showWith(this);"/></div>
				</logic:present>
			</div>	
		
			<% if (action.equalsIgnoreCase("create")) {%>
			<logic:present name="<%=ActionConst.Visitor_Is_Owner %>">
			<!-- following part can only be viewed by the item owner -->
			<div id="planNew" class="section" style="display: none"><div class="sub-section">
				<logic:notPresent name="goals">
				<span class="prompt-message">没有可执行目标，您需要先<a href='goal.do?act=create'>创建一个新目标</a>然后才能创建计划</span>
				</logic:notPresent>
				
				<table class="life-table">
					<tr>
						<td class="row-td">选择目标</td>
						<td class="input-td" colspan="3">
							<select id="newPlanGoal" class="input-text" style="width:404px;">
								<option value='0'>请选择</option>
								<logic:present name="goals">
								<logic:iterate id="goal" name="goals">
									<option value="<bean:write name='goal' property='id'/>"><bean:write name="goal" property="name"/></option>
								</logic:iterate>	
								</logic:present>							
							</select>
							<script type="text/javascript">$("newPlanGoal").value = "<%=request.getParameter(ActionConst.Parent_Id_In_Request) %>"</script>
						</td>
					</tr>
					<tr>
						<td class="row-td">计划名称</td><td class="input-td" colspan="3"><input id="newName" type="text" class="input-text" style="width:402px;" title="计划名称(1~50)" maxlength="50"/></td>
					</tr>
					<tr>
						<td class="row-td">预定开始日期</td>
						<td class="input-td"><input id="newStartDate" type="text" class="shortDate" maxlength="10" title="预定开始日期(yyyy-MM-dd)" onfocus="datePicker.showWith(this);"/></td>
						<td class="row-td">预定结束日期</td>
						<td class="input-td"><input id="newEndDate" type="text" class="shortDate" maxlength="10" title="预定结束日期(yyyy-MM-dd)" onfocus="datePicker.showWith(this);"/></td>
					</tr>	
					<tr>
						<td class="row-td">计划内容</td>
						<td class="input-td" colspan="3"><textarea id="newContent" class="input-text" style="width:402px;height:100px;" title="计划内容(1~500)"></textarea></td>
					</tr>	
					<jsp:include page="/pagelet/accessEditPagelet.jsp"/>
					<tr>
						<td colspan="4"><input id="createPlan" type="button" class="confirmBtn" value="保存" onclick="createPlan();"/>
							<input type="button" class="cancelBtn" value="取消" onclick="window.location.href='plan.do?act=all&uid=<%=user_req_id %>';"/>
						</td>
					</tr>
				</table>
			</div></div>
			</logic:present>
			<%} if (action.equalsIgnoreCase("view")) {%>
			<logic:notPresent name="plan">
				<div class="section"><span class="info-message">您查看的计划不存在，可能已被删除，或者您输入的URL有误</span></div>
			</logic:notPresent>
			
			<logic:present name="plan">
			<div id="planView" class="section" style="display: none"><div class="sub-section">
				<logic:present name="plan">	
				<bean:define id='status' name='plan' property='status'/>		
				<table class="life-table">
					<tr>
						<td class="row-td">计划名称</td><td colspan="3" class="input-td"><span id="viewName" class="desc"><bean:write name='plan' property='name'/></span></td>
					</tr>					
					<tr>
						<td class="row-td">所属目标</td>
						<td colspan="3" class="input-td" width="240px">
							<input id="goalId" type="hidden" value="<bean:write name='plan' property='goalId'/>"/>
							<a href="goal.do?act=view&uid=<%=user_req_id %>&id=<bean:write name='plan' property='goalId'/>"><bean:write name='plan' property='goalName'/></a>
						</td>
					</tr>
					<tr>
						<td class="row-td">计划内容</td><td colspan="3" class="input-td"><span id="viewContent"><bean:write filter="false" name='plan' property='content'/></span></td>
					</tr>					
					<tr>
						<td class="row-td">当前状态</td><td class="input-td"><span id="viewStatus"><%=RdcResourceBundle.LIFEGOAL_STATUS_DESCRIPTIONS[(Integer)status]%></span></td>	
						<td class="row-td">当前进度</td><td class="input-td"><bean:write name='plan' property='progress'/>%</td>	
					</tr>
					<tr>
						<td class="row-td">任务数</td><td class="input-td"><a href="task.do?act=all&uid=<%=user_req_id %>&pid=<%=plan_req_id %>" title="查看所有任务">共<bean:write name='plan' property='taskCount'/>个</a></td>
						<td class="row-td">已分配量</td><td class="input-td"><bean:write name='plan' property='assignedProportion'/>%</td>	
					</tr>
					<tr>
						<td class="row-td">预定开始日期</td><td class="input-td"><span id="viewStartDate"><bean:write name='plan' property='scheduledStartDate'/></span></td>	
						<td class="row-td">预定结束日期</td><td class="input-td"><span id="viewEndDate"><bean:write name='plan' property='scheduledEndDate'/></span></td>	
					</tr>
					<tr>
						<td class="row-td">实际开始时间</td><td class="input-td" width="240px"><span id="realStartDate"><bean:write name='plan' property='realStartDate'/></span></td>
						<td class="row-td">实际结束时间</td><td class="input-td"><span id="realEndDate"><bean:write name='plan' property='realEndDate'/></span></td>						
					</tr>										
					<tr>
						<td class="row-td">创建时间</td><td class="input-td"><span id="viewCreated"><bean:write format="yyyy-MM-dd HH:mm:ss" name='plan' property='createdTime'/></span></td>
						<td class="row-td">上次更新时间</td><td class="input-td"  width="150px"><span id="viewUpdated"><bean:write format="yyyy-MM-dd HH:mm:ss" name='plan' property='updatedTime'/></span></td>
					</tr>
					
					<logic:present name="<%=ActionConst.Visitor_Is_Owner %>">
					<tr>
						<td class="row-td">访问权限</td><td class="input-td" colspan="3"><span id='viewVisibility'><bean:write name='plan' property='visibilityDesc'/></span></td>
					</tr>
					<tr>
						<td colspan="4">
							<input type="button" class="confirmBtn" value="编辑" onclick="goToEdit();"/>
							<input type="button" class="cancelBtn" value="关闭" onclick="window.location.href='plan.do?act=all&uid=<%=user_req_id %>';"/>
						</td>
					</tr>
					</logic:present>
					
				</table>
				</logic:present>
			</div></div>
			
			<logic:present name="<%=ActionConst.Visitor_Is_Owner %>">
			<div id="planEdit" class="section" style="display: none"><div class="sub-section">
				<table class="life-table">
					<tr>
						<td class="row-td">计划名称</td>
						<td class="input-td" colspan="3">
							<input id="editName" type="text" class="input-text" style="width:402px;" maxlength="50" title="计划名称(1~50)" />
						</td>
					</tr>
					<tr>
						<td class="row-td">计划内容</td>
						<td class="input-td" colspan="3"><textarea id="editContent" class="input-text" style="width:402px;height:100px;" title="计划内容(1~500)"></textarea></td>
					</tr>				
					<tr>
						<td class="row-td">预定开始日期</td>
						<td class="input-td"><input id="editStartDate" type="text" class="shortDate" maxlength="10" title="预定开始日期(yyyy-MM-dd)" onfocus="datePicker.showWith(this);"/></td>
						<td class="row-td">预定结束日期</td>
						<td class="input-td"><input id="editEndDate" type="text" class="shortDate" maxlength="10" title="预定结束日期(yyyy-MM-dd)" onfocus="datePicker.showWith(this);"/></td>
					</tr>
					<jsp:include page="/pagelet/accessEditPagelet.jsp"/>
					<tr>
						<td colspan="4">							
							<input id="savePlan" type="button" class="confirmBtn" value="保存" onclick="savePlan();"/>
							<input type="button" class="cancelBtn" value="取消" onclick="showSection('', 'planView');"/>
						</td>
					</tr>
				</table>
			</div></div>
			</logic:present>
			
			<div id="taskExecSec" class="section" style="display: none"><div style="float:left;">
				这里可以
				<logic:present name="<%=ActionConst.Visitor_Is_Owner %>">
				<a href="task.do?act=create&pid=<%=plan_req_id %>">为该计划创建任务</a>
				或者
				</logic:present>				
				<a href="task.do?act=all&uid=<%=user_req_id %>&pid=<%=plan_req_id %>">查看该计划的所有任务</a>
			</div></div>
			
			<!-- show comments -->
			<jsp:include page="/pagelet/commentPagelet.jsp"/>
			
			</logic:present>
			
			<%} // view plan %>
		</div>
	
		<!-- right part: -->
		<div class="right-part">
		</div>
	
	</div>
	
	<div class="clear-div"></div>
	</div>
	
	<jsp:include page="/res/jsp/footer.jsp"></jsp:include>


<script type="text/javascript">
	// var titles = new Array("view-all", "new-plan", "view-plan", "edit-plan");
	var sections = ["planNew", "planView", "planEdit"];
	var planId = "<%= plan_req_id%>"; // which plan is chosen
	var req_uid = "<%=user_req_id%>";
	var ssn_uid = "<%=user_ssn_id%>";
	var dropMenu = null;
	var commentUtil = null;
	
	window.onload = function(){
		var action = "<%=request.getParameter(ActionConst.Method_Symbol)%>";
		if(action != "create" && action != "view"){
			PromptDialog.show("您输入的网址有误");
		}
		else if(action == "create" && typeof goToCreate != "undefined"){
			goToCreate();
		}
		else {
			if(typeof createDropMenu != "undefined"){
				createDropMenu();
			}
			commentUtil = new CommentUtil("LifePlan", planId, ssn_uid, req_uid);
			RDC.gui.display("planView");
			RDC.gui.display("taskExecSec");
		}
	}
	
	function goToCreate(){	
		setCurPlan(null);	
		$("newName").value = "";
		var dt = RDC.util.dateToString(new Date(), RDC.jsconst.DATE_FORMAT_yMd);
		$("newStartDate").value = dt;
		$("newEndDate").value = dt;
		$("newContent").value = "";
		showSection("", "planNew");
		$("newPlanGoal").focus();
		RDC.gui.hide("btnSubSec");
	}
	
</script>
<logic:present name="<%=ActionConst.Visitor_Is_Owner %>">
<script type="text/javascript">	
	var datePicker = new DatePicker();
	
	function createDropMenu(){
		if(!RDC.gui.exist("btnSubSec")){
			return;
		}
		
		dropMenu = new DropMenu("60px");
		//var dropMenu = new DropMenu($("btnSubSec"), "更多操作", "60px");
		var item = dropMenu.createItem("新建");
		item.id = "item_create";
		item.onclick = function(){
			window.location.href = "plan.do?act=create";
		};
		dropMenu.addItem(item);
		
		item = dropMenu.createItem("删除");
		item.id = "item_delete";
		item.onclick = function(){
			goToDelete();
		};
		dropMenu.addItem(item);
	}
	
	function setCurPlan(pid){
		planId = pid;
	}
	
	function getCurPlan(){
		return planId;
	}
	
	function showSection(title, sec){
		for(var i = 1; i < sections.length; i++){
			if(sec != sections[i]){
				RDC.gui.hide(sections[i]);
			}
		}
		RDC.gui.display(sec);
	}
	
	function goToEdit(){
		$("editName").value = $("viewName").innerHTML;
		$("editStartDate").value = $("viewStartDate").innerHTML;
		$("editEndDate").value = $("viewEndDate").innerHTML;
		$("editContent").value = $("viewContent").innerHTML;
		$("limitedAccess").value = $("viewVisibility").innerHTML;
		showSection("", "planEdit"); 
		$("editName").focus();
	}
	
	function checkName(value){
		if(RDC.util.verifyText(value, 1, 50) == false){
			PromptDialog.show("计划名称的字数在1~50之间");
			return false;
		}
		return true;
	}
	
	function checkContent(value){
		if(RDC.util.verifyText(value, 1, 500) == false){
			PromptDialog.show("计划内容的字数在1~500之间");
			return false;
		}
		return true;
	}
	
	function checkNewStartDate(elemId){
		if(RDC.util.verifyPastDate($(elemId).value, RDC.jsconst.DATE_FORMAT_yMd)){
			PromptDialog.show("开始日期格式不合法或是过去某个日期");
			return false;
		}
		return true;
	}
	
	function checkNewEndDate(startId, endId){
		if(RDC.util.verifyPastDate($(endId).value, RDC.jsconst.DATE_FORMAT_yMd)){
			PromptDialog.show("开始时间格式不合法或不是未来时间");
			return false;
		}
		else if(RDC.util.compareTwoDates($(startId).value, $(endId).value) == 1){
			PromptDialog.show("预定结束日期不应早于预定开始日期");
			return false;
		}
		return true;
	}
	
	function checkNewInputs(){
		if($("newPlanGoal").selectedIndex < 1){
			PromptDialog.show("请选择一个目标");
			return false;
		}		
		if(checkName($("newName").value) == false){
			return false;
		}
		if(checkNewStartDate("newStartDate") == false){
			return false;
		}
		if(checkNewEndDate("newStartDate", "newEndDate") == false){
			return false;
		}
		if(checkContent($("newContent").value) == false){
			return false;
		}
	}
	
	function createPlan(){		
		if(checkNewInputs() == false)
			return;

		var plan = {id:null};
		plan.aspirantId = ssn_uid;
		plan.goalId = $("newPlanGoal").options[$("newPlanGoal").selectedIndex].value;
		plan.name = $("newName").value;
		plan.content = $("newContent").value;
		plan.scheduledStartDate = RDC.util.stringToDate($("newStartDate").value, RDC.jsconst.DATE_FORMAT_yMd);
		plan.scheduledEndDate = RDC.util.stringToDate($("newEndDate").value, RDC.jsconst.DATE_FORMAT_yMd);
		plan.visibility = $("limitedAccess").selectedIndex;
		RDC.gui.disable("createPlan");
		PlanHandler.savePlan(plan, function(result){
			if(result == null){
				PromptDialog.show("保存计划失败，请稍后再试");
				RDC.gui.enable("createPlan");
			}
			else {
				window.location.href = "plan.do?act=view&uid=" + req_uid + "&id=" + result.id;
			}
		});
	}
	
	function checkEditStartDate(elemId){
		if(RDC.util.isValidDate($(elemId).value, RDC.jsconst.DATE_FORMAT_yMd) == false){
			PromptDialog.show("预定开始日期格式不合法");
			return false;
		}
		//else if(compareDates($(elemId).value, RDC.jsconst.DATE_FORMAT_yMd, $("gdate" + getCurPlan()).value, RDC.jsconst.DATE_FORMAT_yMd) == 1){
		//	RDC.gui.showMsg("planMsg", "预定结束日期不应早于目标预定达到日期");
		//	return false;
		//}
		return true;
	}
	
	function checkEditEndDate(startId, endId){
		if(RDC.util.isValidDate($(endId).value, RDC.jsconst.DATE_FORMAT_yMd) == false){
			PromptDialog.show("预定结束日期格式不合法");
			return false;
		}
		else if(RDC.util.compareTwoDates($(startId).value, $(endId).value) == 1){
			PromptDialog.show("预定结束日期不应早于预定开始日期");
			return false;
		}
		//else if(compareDates($(elemId).value, RDC.jsconst.DATE_FORMAT_yMd, $("gdate" + getCurPlan()).value, RDC.jsconst.DATE_FORMAT_yMd) == 1){
		//	RDC.gui.showMsg("planMsg", "预定结束日期不应早于目标预定达到日期");
		//	return false;
		//}
		return true;
	}
	
	function checkEditInputs(){
		if(checkName("editName") == false){
			return false;
		}
		if(checkEditStartDate("editStartDate") == false){
			return false;
		}
		if(checkEditEndDate("editStartDate", "editEndDate") == false){
			return false;
		}
		if(checkContent("editContent") == false){
			return false;
		}
		return true;
	}
	
	function savePlan(){
		if(checkEditInputs() == false)
			return;
			
		var plan = {id:getCurPlan()};
		plan.aspirantId = ssn_uid;
		plan.goalId = $("goalId").value;
		plan.name = $("editName").value;
		plan.content = $("editContent").value;
		var dt = $("realStartDate").innerHTML;
		if(RDC.util.isValidDate(dt, RDC.jsconst.DATE_FORMAT_yMd)){
			plan.realStartDate = RDC.util.stringToDate(dt, RDC.jsconst.DATE_FORMAT_yMd);
			
			dt = $("realEndDate").innerHTML;
			if(RDC.util.isValidDate(dt, RDC.jsconst.DATE_FORMAT_yMd)){
				plan.realEndDate = RDC.util.stringToDate(dt, RDC.jsconst.DATE_FORMAT_yMd);
			}
		}
		plan.scheduledStartDate = RDC.util.stringToDate($("editStartDate").value, RDC.jsconst.DATE_FORMAT_yMd);
		plan.scheduledEndDate = RDC.util.stringToDate($("editEndDate").value, RDC.jsconst.DATE_FORMAT_yMd);
		plan.createdTime = RDC.util.stringToDate($("viewCreated").innerHTML, RDC.jsconst.DATE_FORMAT_yMdHms);
		plan.visibility = $("limitedAccess").selectedIndex;
		RDC.gui.disable("savePlan");
		PlanHandler.savePlan(plan, function(result){
			if(result == null){
				PromptDialog.show("保存计划失败，请稍后再试");
			}
			else {
				dwr.util.setValue("viewContent", $("editContent").value);
				dwr.util.setValue("viewName", $("editName").value);
				dt = RDC.util.dateToString(result.updatedTime, RDC.jsconst.DATE_FORMAT_yMdHms);
				dwr.util.setValue("viewUpdated", dt);
				dt = RDC.util.dateToString(result.scheduledStartDate, RDC.jsconst.DATE_FORMAT_yMd);
				dwr.util.setValue("viewStartDate", dt);
				dt = RDC.util.dateToString(result.scheduledEndDate, RDC.jsconst.DATE_FORMAT_yMd);
				dwr.util.setValue("viewEndDate", dt);
				dwr.util.setValue("viewVisibility", $("limitedAccess").value);
				showSection("", "planView");
			}
			RDC.gui.enable("savePlan");
		});
	}
	
	function goToDelete(){
		var pid = getCurPlan();
		if(pid == null || pid == ""){
			PromptDialog.show("计划不存在");
			return;
		}
		
		var plan = "<b>" + $("viewName").innerHTML + "</b>";
		var popupWin = new PopUpWindow("信息提示", "您确定要删除计划'" + plan + "'吗？<br>删除此计划同时也会删除计划的所有任务", 0);
		popupWin.addAction(deleteAction(popupWin, pid));
		popupWin.addAction(closeAction(popupWin));
		popupWin.show();
	}
	
	// actions
	function closeAction(dlg){
		var btn = document.createElement("input");		
		btn.type = "button";
		btn.className = "cancelBtn";
		btn.value = "返回";
		btn.onclick = function(){
			dlg.dispose();
		};		
		return btn;
	}
	
	function deleteAction(dlg, pid){
		var btn = dlg.createAction("确定");
		btn.id = "confirm_delete";
		btn.onclick = function(){
			RDC.gui.disable(this.id);
			gid = $("goalId").value;
			PlanHandler.deletePlan(ssn_uid, gid, pid, function(result){
				if(result == false){
					dlg.updateMessage("删除计划失败，请稍后再试");
					RDC.gui.enable(this.id);
				}
				else {
					window.location.href = "plan.do?act=all&uid=" + req_uid;
				}
			});
		}
		return btn;
	}

</script>
</logic:present>

</body>

</html>
